//
//  FCDateTime.swift
//  miaoyin
//
//  Created by David on 2017/2/21.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

func softDate(_ timeStamp: String) -> String?{
    let timeInterval = Double(timeStamp)
    if timeInterval == nil{
        return nil
    }
    let date = Date(timeIntervalSince1970: timeInterval!)
    
    
//    let formatter = DateFormatter()
    
//    formatter.dateFormat = "yyyy-MM-dd"
//    let date = formatter.date(from: timeString)
//    if date == nil{
//        return nil
//    }
    let curDate = Date()
    
    let das = Calendar.current
    //new 一个 NSCalendar
    let flags: NSCalendar.Unit = [.year, .month, .day, .weekday]
    //设置格式
    let nowCom = (das as NSCalendar).components(flags, from: curDate)
    
    let timeCom = (das as NSCalendar).components(flags, from: date)
    //创建当前和需要计算的components
    //components有之前设置的格式的各种参数
    
    if timeCom.year == nowCom.year{
        if timeCom.month == nowCom.month{
            if timeCom.day == nowCom.day{
                return "今天"
            }else{
                if nowCom.day! - timeCom.day! == 1{
                    return "昨天"
                }else if nowCom.day! - timeCom.day! <= 6{
                    
                    return "本周"
                }
            }
        }
        
        if timeCom.day != nil && timeCom.month != nil{
            var showMonth = "\(timeCom.month!)"
            var showDay = "\(timeCom.day!)"
            if timeCom.month! < 10 {
                showMonth = "0\(timeCom.month!)"
            }
            if timeCom.day! < 10{
                showDay = "0\(timeCom.day!)"
            }
            
            return "\(showMonth)-\(showDay)"
        }
        return nil
        
        
    }else{
        if timeCom.day != nil && timeCom.month != nil && timeCom.year != nil{
            var showMonth = "\(timeCom.month!)"
            var showDay = "\(timeCom.day!)"
            if timeCom.month! < 10 {
                showMonth = "0\(timeCom.month!)"
            }
            if timeCom.day! < 10{
                showDay = "0\(timeCom.day!)"
            }
            
            return "\(timeCom.year!)-\(showMonth)-\(showDay)"
        }
        return nil
    }
}
